package com.photoeditor.demo.ui.widget.image.compose;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.graphics.RectF;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.View;

import com.common.base.ComponentContext;
import com.photoeditor.R;


/**
 * 选择颜色的游标
 */
public class CircleColorCursorView extends View {
    private RectF mRectF;
    private Paint mPaint;

    private Drawable mRectDrawable;

    private int mHeight;
    private int mWidth;

    private RectF mDrawableRect;

    public CircleColorCursorView(Context context) {
        super(context);
        init();
    }

    public CircleColorCursorView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public CircleColorCursorView(Context context, AttributeSet attrs,
                                 int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        setWillNotDraw(false);
        mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mPaint.setColor(Color.WHITE);
        mPaint.setStyle(Style.FILL);
        mRectDrawable = ComponentContext.getContext().getResources().getDrawable(R.drawable.ic_get_color_rect);
        mHeight = mRectDrawable.getIntrinsicHeight();
        mWidth = mRectDrawable.getIntrinsicWidth();
        mDrawableRect = new RectF();
        mRectF = new RectF();
        setLayerType(View.LAYER_TYPE_SOFTWARE, null);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.drawCircle(mRectF.centerX(), mRectF.centerX(), mRectF.width() / 2, mPaint);
        mRectDrawable.draw(canvas);
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        if (mRectF != null) {
            mRectF.left = 0;
            mRectF.top = 0;
            mRectF.right = w;
            mRectF.bottom = h;

            mDrawableRect.left = mRectF.centerX() - mWidth / 2;
            mDrawableRect.top = mRectF.centerX() + mRectF.width() / 2;
            mDrawableRect.right = mDrawableRect.left + mWidth;
            mDrawableRect.bottom = mDrawableRect.top + mHeight;

            mRectDrawable.setBounds((int) (mDrawableRect.left + 0.5f), (int) (mDrawableRect.top + 0.5f), (int) (mDrawableRect.right + 0.5f), (int) (mDrawableRect.bottom + 0.5f));
        }
    }

    public void setColor(int color) {
        mPaint.setColor(color);
        invalidate();
    }

    public int getColor() {
        return mPaint.getColor();
    }
}
